package top.zuli.package01.class14;

/**
 * 生产者(Productor)将产品交给店员(Clerk)，而消费者(Customer)从店员处 取走产品，
 * 店员一次只能持有固定数量的产品(比如:20），如果生产者试图生产更多的产品，店员会叫生产者停一下，
 * 如果店中有空位放产品了再通知生产者继续生产；如果店中没有产品了，店员会告诉消费者等一下，
 * 如果店中有产品了再通知消费者来取走产品。
 */

public class ProductorDemo {
    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        Thread t1 = new Productor(clerk);
        Thread t2 = new Customer(clerk);
        t1.start();
        t2.start();
    }

}

//定义店员
class Clerk{
    private  int productCount = 0;
    //生产方法
    public synchronized void shengChan() {
        if(productCount<20){
            productCount++;
            System.out.println(Thread.currentThread().getName()+"开始生产第"+productCount+"个产品");
            notify();
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //消费方法
    public synchronized void xiaoFei() {
        if(productCount>0){
            productCount--;
            System.out.println(Thread.currentThread().getName()+"开始消费第"+productCount+"个产品");
            notify();
        }else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

//定义生产者
class Productor extends Thread{
    private Clerk clerk;
    public Productor(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println("开始生产产品");
        while(true){
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.shengChan();
        }
    }
}

//定义消费者
class Customer extends Thread{
    private Clerk clerk;
    public Customer(Clerk clerk) {
        this.clerk = clerk;
    }
    @Override
    public void run() {
        System.out.println("开始消费产品");
        while(true){
            try {
                sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.xiaoFei();
        }
    }
}

/*
public class ProductorDemo {
    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        Thread t1 = new Productor(clerk);
        Thread t2 = new Customer(clerk);
        t1.start();
        t2.start();
    }

}
class Clerk{
    private  int productCount = 0;
    public synchronized void shengChan() {
        if(productCount<20){
            productCount++;
            System.out.println(Thread.currentThread().getName()+"开始生产第"+productCount+"个产品");
            notify();
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void xiaoFei() {
        if(productCount>0){
            productCount--;
            System.out.println(Thread.currentThread().getName()+"开始消费第"+productCount+"个产品");
            notify();
        }else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
class Productor extends Thread{
    private Clerk clerk;
    public Productor(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println("开始生产产品");
        while(true){
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.shengChan();
        }
    }
}
class Customer extends Thread{
    private Clerk clerk;
    public Customer(Clerk clerk) {
        this.clerk = clerk;
    }
    @Override
    public void run() {
        System.out.println("开始消费产品");
        while(true){
            try {
                sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.xiaoFei();
        }
    }
}*/

/*
public class ProductorDemo {
    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        Thread t1 = new Productor(clerk);
        Thread t2 = new Customer(clerk);
        t1.start();
        t2.start();
    }

}
class Clerk{
    private  int productCount = 0;
    public synchronized void shengChan() {
        if(productCount<20){
            productCount++;
            System.out.println(Thread.currentThread().getName()+"开始生产第"+productCount+"个产品");
            notify();
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void xiaoFei() {
        if(productCount>0){
            productCount--;
            System.out.println(Thread.currentThread().getName()+"开始消费第"+productCount+"个产品");
            notify();
        }else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
class Productor extends Thread{
    private Clerk clerk;
    public Productor(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println("开始生产产品");
        while(true){
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.shengChan();
        }
    }
}
class Customer extends Thread{
    private Clerk clerk;
    public Customer(Clerk clerk) {
        this.clerk = clerk;
    }
    @Override
    public void run() {
        System.out.println("开始消费产品");
        while(true){
            try {
                sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.xiaoFei();
        }
    }
}*/
